﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Form1.cs" company="Voogd en Voogd Diensten">
//   Copyright 2008-2009 Voogd en Voogd Diensten
// </copyright>
// <author>Jelle</author>
// <summary>
//   De Form1 class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

#region Using directives
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion

namespace GLCDDesigner
{
    using System.Reflection;

    using GLCDBaseTypes.BaseTypes;
    using GLCDBaseTypes.PluginInterfaces;

    public partial class Form1 : Form
    {
        private int[,] gridArray = new int[10,10]
            {
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
            };

        public Form1()
        {
            InitializeComponent();

            this.initGridArray();

            Assembly moduleAssembly = Assembly.LoadFrom(@"C:\Users\Jelle\Documents\Visual Studio 2010\Projects\GLCDDesigner\GLCDST7565Plugin\bin\Debug\GLCDST7565Plugin.dll");

            Type[] types = moduleAssembly.GetTypes();

            foreach (var instance in
                from type in types
                where type.GetInterface("ILcdPlugin") != null
                select (ILcdPlugin)Activator.CreateInstance(type))
            {
                this.comboBox1.Items.Add(instance);
            }

            comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// Init grid array
        /// </summary>
        public void initGridArray()
        {
            dataGridView1.ColumnCount = gridArray.GetLength(0);

            var rowCount = this.gridArray.GetLength(1);
            var rowLength = this.gridArray.GetLength(0);

            dataGridView1.Rows.Clear();

            for (int rowIndex = 0; rowIndex < rowCount; ++rowIndex)
            {
                var row = new DataGridViewRow();

                for (int columnIndex = 0; columnIndex < rowLength; ++columnIndex)
                {
                    row.Cells.Add(new DataGridViewTextBoxCell()
                    {
                        Value = gridArray[columnIndex, rowIndex]
                    });
                }

                dataGridView1.Rows.Add(row);
            }

            foreach (DataGridViewColumn column in dataGridView1.Columns)
            {
                column.Width = 20;
            }
        }

        /// <summary>
        /// Button 1_ click
        /// </summary>
        /// <param name="sender">De Sender param van het type object</param>
        /// <param name="e">De E param van het type EventArgs</param>
        private void button1_Click(object sender, EventArgs e)
        {
            decimal dec = decimal.Parse(textBox2.Text);

            richTextBox1.Text += "#.## " + dec.ToString("#.##") + "\n";
            richTextBox1.Text += "N2 " + dec.ToString("N2") + "\n";
            richTextBox1.Text += "#0.00 " + dec.ToString("#0.00") + "\n";


            //string myBinary = this.richTextBox2.Text;

            //richTextBox1.Text = "static unsigned char __attribute__ ((progmem)) logo16_glcd_bmp[]={\n";

            //// chunks of 8 bits
            //while (myBinary.Length > 0)
            //{
            //    string procstring = myBinary.Substring(0, Math.Min(8, myBinary.Length));

            //    myBinary = myBinary.Length > 8 ? myBinary.Substring(8) : string.Empty;

            //    if (procstring.Length < 8)
            //    {
            //        procstring = procstring.PadRight(8, '0');
            //    }

            //    string hex = string.Format("0x{0:X2}", Convert.ToInt32(procstring, 2));

            //    richTextBox1.Text += hex.ToLower() + ", ";
            //}

            //richTextBox1.Text += "};";
        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            // 
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1))
            {
                if (e.Button == MouseButtons.Left)
                {
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = 1;
                }
                else
                {
                    dataGridView1[e.ColumnIndex, e.RowIndex].Value = 0;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // Lees hex en probeer om te zetten naar binair
            List<HexRepresentationItem> hexItems = new List<HexRepresentationItem>();
            List<string> parsedHex = richTextBox2.Text.Split(',').ToList();

            foreach (string s in parsedHex)
            {
                HexRepresentationItem hexItem = new HexRepresentationItem();
                hexItem.HexValue = s;

                hexItems.Add(hexItem);
            }

            Bitmap bmp = ((ILcdPlugin)comboBox1.SelectedItem).CreateBitmapFromHex(hexItems, (int)numericUpDown1.Value, (int)numericUpDown2.Value);
            pictureBox1.Image = bmp;

            richTextBox1.Text = ((ILcdPlugin)comboBox1.SelectedItem).GetArduinoCode(bmp, textBox2.Text);

            List<HexRepresentationItem> hexItems2 = ((ILcdPlugin)comboBox1.SelectedItem).GetHexList(bmp);

            //int newline = 0;
            //foreach (HexRepresentationItem hexRepresentationItem in hexItems2)
            //{
            //    if (newline == 2)
            //    {
            //        richTextBox1.Text += "\n";
            //        newline = 0;
            //    }

            //    richTextBox1.Text += hexRepresentationItem.ToBinary() + " " + hexRepresentationItem.HexValue + " ";

            //    newline++;
            //}

            Bitmap bmp2 = ((ILcdPlugin)comboBox1.SelectedItem).CreateBitmapFromHex(hexItems2, (int)numericUpDown1.Value, 4);
            pictureBox2.Image = bmp2;

            //List<HexRepresentationItem> hexItems = 


            //List<string> parsedHex = textBox1.Text.Split(',').ToList();

            //richTextBox1.Text = string.Empty;

            //foreach (string s in parsedHex)
            //{
            //    // strip '0x'
            //    string hex = s.Trim().Substring(2);
            //    string binaryText = Convert.ToString(Convert.ToInt32(hex, 16), 2).PadLeft(8, '0');

            //    richTextBox1.Text += binaryText + "\n";
            //}

            //Bitmap bitmap = new Bitmap(1000, 1000);
            //Graphics graphic = Graphics.FromImage(bitmap);

            //Brush blackPen = new SolidBrush(Color.Black);
            //Pen whitePen = new Pen(Color.White);

            //// We kennen nu het aantal regels en kolommen
            //int regels = richTextBox1.Lines.Count();
            //int kolommen = richTextBox1.Lines[0].Length;

            //this.gridArray = new int[regels, kolommen];

            //// En vullen
            //int regel = 0;
            //foreach (string line in richTextBox1.Lines)
            //{
            //    int col = 0;
            //    foreach (char c in line)
            //    {
            //        this.gridArray[regel, col] = c == '1' ? 1 : 0;

            //        col++;
            //    }

            //    regel++;
            //}

            //this.initGridArray();

            //// De karakters zijn gekanteld!
            //// dus niet x,y maar y,x
            //// De regels lopen dus naar beneden, zo moet het grid ook geintepreteerd worden

            //// ST7565 LCD is 8 regels van 16 en dan opnieuw (memory blocks)
            //// Dus bij 8 * 16 'wrap' naar start            
            //int xoffset = 0;
            //int teller = 0;

            //int y = 0;
            //foreach (string s in parsedHex)
            //{
            //    if (teller == numericUpDown1.Value)
            //    {
            //        xoffset = xoffset + 8;
            //        y = 0;
            //        teller = 0;
            //    }

            //    // strip '0x'
            //    string hex = s.Trim().Substring(2);
            //    string binaryText = Convert.ToString(Convert.ToInt32(hex, 16), 2).PadLeft(8, '0');

            //    for (int x = 1; x <= binaryText.Length; x++)
            //    {
            //        if (binaryText[x - 1] == '1')
            //        {
            //            graphic.FillRectangle(blackPen, y * 4, (xoffset + x) * 4, 4, 4);
            //            //graphic.FillRectangle(blackPen, (xoffset + x) * 4, y * 4, 4, 4);
            //        }
            //    }

            //    y++;
            //    teller++;
            //}

            //pictureBox1.Image = bitmap;
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if ((e.ColumnIndex > -1) && (e.RowIndex > -1))
            {
                if (e.Value != null)
                {
                    if ((int)e.Value == 1)
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.Black), e.CellBounds);
                    }
                    else
                    {
                        e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);
                    }

                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
            }
            else
            {
                e.Handled = false;
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() != System.Windows.Forms.DialogResult.Cancel)
            {
                Bitmap bmp = new Bitmap(openFileDialog1.FileName);

                pictureBox1.Image = bmp;

                richTextBox1.Text = ((ILcdPlugin)comboBox1.SelectedItem).GetArduinoCode(bmp, textBox2.Text);

                List<HexRepresentationItem> hexItems2 = ((ILcdPlugin)comboBox1.SelectedItem).GetHexList(bmp);

                Bitmap bmp2 = ((ILcdPlugin)comboBox1.SelectedItem).CreateBitmapFromHex(hexItems2, bmp.Width, (int)numericUpDown2.Value);
                pictureBox2.Image = bmp2;

            }
        }
    }
}
